package tech.aistar.controller.order;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import tech.aistar.entity.*;
import tech.aistar.service.order.IDemandOrderService;
import tech.aistar.service.order.IServiceOrderService;
import tech.aistar.service.services.IServicesService;
import tech.aistar.util.DateUtileDemo;
import tech.aistar.vo.DemandOrderVo;
import tech.aistar.vo.ServiceOrderVo;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

/**
 * Created by 23827 on 2019/9/30.
 */
@Controller
@RequestMapping("/demand")
public class DemandController {

    @Autowired
    private IDemandOrderService demandOrderService;
    @Autowired
    private IServicesService iServicesService;

    //作为需求者 - 发布需求和预约服务

    //发布的需求
    //展示发布的需求订单,并且分不同状态展示
    @RequestMapping("/release")
    public String demand(Model model,HttpSession session){

        List<DemandOrderVo> demandOrderVoList = demandOrderService.demandOrderVo();

        Iterator<DemandOrderVo> iter = demandOrderVoList.iterator();
        while(iter.hasNext()){
            DemandOrderVo demandOrderVo = iter.next();
            Date time = demandOrderVo.getCommitTime();
            String ctime = DateUtileDemo.format("yyyy-MM-dd HH:mm:ss",time);
            session.setAttribute("ctime",ctime);
        }

        model.addAttribute("demandOrderVoList",demandOrderVoList);


        return "order/releaseDemandOrder";
    }

    //预约服务
    @Autowired
    private IServiceOrderService iServiceOrderService;
    @RequestMapping("/reserved")
    public String list(Model model, HttpSession session){

        Integer uid = (Integer) session.getAttribute("userId");
        List<ServiceOrderVo> serviceOrderVoList = iServiceOrderService.serviceOrderVo(uid);
        Iterator<ServiceOrderVo> iter =serviceOrderVoList.iterator();
        while(iter.hasNext()){
            ServiceOrderVo serviceOrderVo = iter.next();
//            System.out.println(serviceOrderVo);
//            System.out.println("gggggggggggggggggggggggggggg");
            Date time = serviceOrderVo.getCommitTime();
            String dtime = DateUtileDemo.format("yyyy-MM-dd HH:mm:ss",time);
            session.setAttribute("dtime",dtime);
        }
        session.setAttribute("serviceOrderVoList",serviceOrderVoList);


//      int i =iServiceOrderService.findByOrderNumber("201909281550");
//      ServiceOrder serviceOrder=  iServiceOrderService.findByOrderNumber("201909281550");
//        ServiceOrder s = new ServiceOrder();
//        s.setId(i);
//        s.setUserinfoId(uid);
//        s.setStatus(3);
//        s.setServicesId(serviceOrder.getServicesId());

//        iServiceOrderService.updateServiceOrder(s);
        return "order/reservedServiceOrder";
    }


    //取消预约的服务订单
    @RequestMapping("/del")
    public String del(Model model,Integer id,HttpSession session){
        ServiceOrder serviceOrder = iServiceOrderService.delById(id);
        model.addAttribute("serviceOrder",serviceOrder);
        Integer uid = (Integer) session.getAttribute("userId");
        List<ServiceOrderVo> serviceOrderVoList = iServiceOrderService.serviceOrderVo(uid);
        session.setAttribute("serviceOrderVoList",serviceOrderVoList);
        return "order/reservedServiceOrder";
    }

    //支付服务费用
    @RequestMapping("/cost")
    public String cost(Model model,Integer id,HttpSession session){
//        System.out.println(id);
//        ServiceOrder serviceOrder = iServiceOrderService.findByIdOne(id);
        ServiceOrder serviceOrder = iServiceOrderService.findByIdOne(id);
        Services services = iServicesService.findById(serviceOrder.getServicesId());
        System.out.println(services);
        System.out.println(serviceOrder);
        model.addAttribute("services",services);

//        serviceOrder.getTotal();
//        System.out.println(serviceOrder.getTotal());
//        serviceOrder.getOrderNumber();
        System.out.println(serviceOrder.getOrderNumber());
        Double servicePrice = serviceOrder.getTotal();
        String serviceOrderNum = serviceOrder.getOrderNumber();
        model.addAttribute("serviceOrder",serviceOrder);
        model.addAttribute("serviceOrderNum",serviceOrderNum);
        model.addAttribute("servicePrice",servicePrice);

//        ServiceOrder serviceOrder1 = new ServiceOrder();
        String orderNumber = iServiceOrderService.selectById(id);
        Integer uid = (Integer) session.getAttribute("userId");
        ServiceOrder s = new ServiceOrder();
//        s.setId(id);
        s.setUserinfoId(uid);
        s.setServicesId(services.getId());
        s.setOrderNumber(orderNumber);
        s.setId(id);
        s.setStatus(2);
//        s.setCommitTime(new Date());
        iServiceOrderService.updateOne(s);
//        model.addAttribute("serviceOrder1",serviceOrder1);

        //支付成功成交数加一
        services.setCount(services.getCount()+1);
        services.setStatus(1);
        iServicesService.add(services);

        return "/pay/payPage";
    }


}
